from src.Car import Car
from src.Truck import Truck


class CarsShop:

    def __init__(self, io_strategy, db_strategy, sync_db_strategy):
        self.url = None
        self.catalog = list()
        self.io_strategy = io_strategy
        self.save_file_strategy = db_strategy
        self.sync_db_strategy = sync_db_strategy
        self.path = "data/aam2307/st13/shop.data"
        self.initial_load = False

    def update_buffer(self, parent_url):
        if not self.initial_load:
            self.url = "http://127.0.0.1:5000" + parent_url + "/backround_process"
        self.catalog.clear()
        db_buffer = self.sync_db_strategy.get_items()
        for r in db_buffer:
            self.create_vehicle_in_buffer(type_of_c=r["type"])

        if not self.initial_load:
            self.initial_load = True

    def create_vehicle_in_buffer(self, type_of_c):
        if type_of_c == "truck":
            vehicle = Truck(self.io_strategy, len(self.catalog) + 1)
            self.catalog.append(vehicle)
        elif type_of_c == "car":
            vehicle = Car(self.io_strategy, len(self.catalog) + 1)
            self.catalog.append(vehicle)

    def add_car(self):
        try:
            print("add car")
            c = Car(self.io_strategy, len(self.catalog) + 1)
            self.catalog.append(c)
            return c
        except Exception as e:
            print(e.args[0])

    def add_truck(self):
        try:
            print("add truck")
            t = Truck(self.io_strategy, len(self.catalog) + 1)
            self.catalog.append(t)
            return t
        except Exception as e:
            print(e.args[0])

    def create_any_vehicle(self, type_of_c):
        if type_of_c == "truck":
            v = self.add_truck()
            self.sync_db_strategy.add(v, type_of_c)
        elif type_of_c == "car":
            v = self.add_car()
            self.sync_db_strategy.add(v, type_of_c)
        else:
            print("error: unknown type")

    def show_catalog(self):
        return self.catalog

    def save(self):
        self.save_file_strategy.save(self.path, self.catalog)

    def load(self):
        self.catalog = self.save_file_strategy.load(self.path)

    def load_vehicles(self):
        self.clear_all()
        self.catalog = self.save_file_strategy.s_load(self.path)
        Truck.latest_inventory_number = len(self.catalog)
        for car in self.catalog:
            if hasattr(car, "capacity"):
                self.sync_db_strategy.add(car, "car")
            else:
                self.sync_db_strategy.add(car, "truck")

    def clear_all(self):
        try:
            self.catalog.clear()
            Truck.latest_inventory_number = 1
            self.sync_db_strategy.clear()
            self.sync_db_strategy.__init__(self.sync_db_strategy.sql_path)
        except Exception as e:
            print(e.args[0])

    def edit_car(self, num):
        for i in self.catalog:
            if i.inventory_number == num:
                i.edit()
                if not hasattr(i, "capacity"):
                    print("car")
                    self.sync_db_strategy.change(i, "car")
                else:
                    print("truck")
                    self.sync_db_strategy.change(i, "truck")
                break

    def delete_car(self, num):
        for i in self.catalog:
            if i.inventory_number == num:
                self.catalog.pop(i.inventory_number)
        self.sync_db_strategy.delete(num)

    def get_by_number(self, inv_choice):
        for i in self.catalog:
            if i.inventory_number == inv_choice:
                return i
